<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>Clients List</title>
</h:head>
<body>
	<f:view>
		<p:ajaxStatus id="ajaxStatusPanel" style="position:absolute; top: 50%; left: 50%; z-index: 9999;">
			<f:facet name="start">
				<p:graphicImage value="/resources/images/ajax-loader.gif" />
			</f:facet>
			<f:facet name="complete">
				<h:outputText value="" />
			</f:facet>
		</p:ajaxStatus>

		<h:form id="form">

			<p:growl id="messages" showDetail="true" />

			<p:commandButton id="addNewClientButton" type="button"
				onclick="add.show();" value="Add new client"
				update=":formAdd:addClient" action="#{clientBean.create}" global="true" />
			<br />

			<p:dataTable id="clientsTable" var="client"
				value="#{clientBean.getAllClients()}" border="true">


				<p:column headerText="Client Id">
					<h:outputText value="#{client.clientId}" />
				</p:column>
				<p:column headerText="Name">
					<h:outputText value="#{client.name}" />
				</p:column>
				<p:column headerText="Address">
					<h:outputText value="#{client.address}" />
				</p:column>
				<p:column headerText="Email">
					<h:outputText value="#{client.email}" />
				</p:column>
				<p:column headerText="Options">
					<p:commandButton
						action="#{clientBean.getClientById(client.clientId)}"
						update=":formEdit:editClient" value="Edit" id="editClientButton"
						oncomplete="edit.show();">
					</p:commandButton>
					<p:commandButton
						action="#{clientBean.getClientById(client.clientId)}"
						value="Delete" id="deleteClientButton"
						update=":formDelete:deleteClient" oncomplete="remove.show();"
						icon="ui-icon-circle-close">
					</p:commandButton>
					<p:commandButton action="#{orderBean.getOrders(client.clientId)}"
						value="Orders">
						<f:setPropertyActionListener value="#{client.clientId}"
							target="#{clientBean.client.clientId}" />
					</p:commandButton>
				</p:column>
			</p:dataTable>
			<p:messages/>
		</h:form>
	</f:view>

	<h:form id="formAdd">
		<p:dialog id="addClient" header="Add new client" widgetVar="add"
			modal="true" resizable="false">

			<h:panelGrid columns="2" id="addDialog">
				<h:outputLabel for="name" value="Name: " />
				<p:inputText id="name" value="#{clientBean.client.name}"
					label="name" required="true">

					<f:validateRequired for="name" />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>

				<h:outputLabel for="address" value="Address: " />
				<p:inputText id="address" value="#{clientBean.client.address}"
					label="address" required="true">

					<f:validateRequired for="address" />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>

				<h:outputLabel for="email" value="Email: " />
				<p:inputText id="email" value="#{clientBean.client.email}"
					label="email" required="true">

					<f:validateRequired for="email" />
					<f:validateRegex
						pattern="^[a-zA-Z0-9.!#$%&amp;'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>
			</h:panelGrid>

			<p:commandButton actionListener="#{clientBean.add}" value="Save"
				oncomplete="if(args &amp;&amp; !args.validationFailed) add.hide();"
				update=":form" icon="ui-icon-check" />

			<p:commandButton value="Cancel" onclick="add.hide();"
				icon="ui-icon-close" />
		</p:dialog>
	</h:form>

	<h:form id="formEdit">
		<p:dialog id="editClient" header="Edit client" widgetVar="edit"
			modal="true" resizable="false">

			<h:panelGrid columns="2" id="deleteDialog">
				<h:outputLabel for="id" value="Id: " />
				<h:outputText id="id" value="#{clientBean.client.clientId}"
					label="id" />

				<h:outputLabel for="name" value="Name: " />
				<p:inputText id="name" value="#{clientBean.client.name}"
					label="name">

					<f:validateRequired />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>

				<h:outputLabel for="address" value="Address: " />
				<p:inputText id="address" value="#{clientBean.client.address}"
					label="address">

					<f:validateRequired />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>

				<h:outputLabel for="email" value="Email: " />
				<p:inputText id="email" value="#{clientBean.client.email}"
					label="email">

					<f:validateRequired />
					<f:validateRegex
						pattern="^[a-zA-Z0-9.!#$%&amp;'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" />
					<f:validateLength minimum="5" maximum="45" />
					<p:clientValidator />
				</p:inputText>

			</h:panelGrid>

			<p:commandButton actionListener="#{clientBean.update}" value="Edit"
				oncomplete="if(args &amp;&amp; !args.validationFailed) edit.hide();"
				update=":form" />

			<p:commandButton value="Cancel" onclick="edit.hide();"
				icon="ui-icon-close" />

		</p:dialog>
	</h:form>


	<h:form id="formDelete">
		<p:dialog header="Are you sure you want to delete this client?"
			widgetVar="remove" modal="true" resizable="false">

			<h:panelGrid columns="2" id="deleteClient">
				<h:outputLabel for="id" value="Id: " />
				<h:outputText id="id" value="#{clientBean.client.clientId}"
					label="id" />

				<h:outputLabel for="name" value="Name: " />
				<h:outputText id="name" value="#{clientBean.client.name}"
					label="name" />

				<h:outputLabel for="address" value="Address: " />
				<h:outputText id="address" value="#{clientBean.client.address}"
					label="address" />

				<h:outputLabel for="email" value="Email: " />
				<h:outputText id="email" value="#{clientBean.client.email}"
					label="email" />

			</h:panelGrid>

			<p:commandButton actionListener="#{clientBean.delete}"
				oncomplete="if(args &amp;&amp; !args.validationFailed) remove.hide();"
				value="Delete" icon="ui-icon-circle-close" update=":form" />

			<p:commandButton value="Cancel" onclick="remove.hide();"
				icon="ui-icon-close" />

		</p:dialog>
	</h:form>

</body>
</html>